package com.tarena.shoot;
import java.awt.image.BufferedImage;
import java.util.Random;
/**英雄机：是飞行物*/
public class Hero extends FlyingObject {
	private int life;		//命
	private int doubleFire;	//火力值
	private BufferedImage[] images; //图片数组
	private int index;		//协助图片切换
	/**构造方法*/
	public Hero() {
		image = ShootGame.hero0;
		width = image.getWidth();
		height = image.getHeight();
		Random rand = new Random();
		x = 150;
		y = 400;
		life = 3;
		doubleFire = 10;		//火力值为0，单倍火力
		images = new BufferedImage[] {ShootGame.hero0,ShootGame.hero1};
		index = 0;		//协助切换
	}
	
	public void step() {
		image = images[index++ / 10 % images.length];
	}
	
	/**英雄机发射子弹*/
	public Bullet[] shoot() {
		if(doubleFire == 0) {
			Bullet[] bs= new Bullet[1];
			bs[0] = new Bullet(this.x + this.width / 2, this.y + 10);
			return bs;
		}else {
			Bullet[] bs= new Bullet[2];
			bs[0] = new Bullet(this.x + this.width / 4, this.y + 10);
			bs[1] = new Bullet(this.x + this.width / 4 * 3, this.y + 10);
			doubleFire -= 2;
			return bs;
		}
	}
	
	public void moveTo(int x, int y) {
		this.x = x - width / 2;
		this.y = y - height / 2;
	}
	
	public boolean outOfBounds() {
		return false;
	}
	
	public void addDoubleFire() {
		doubleFire += 40;
	}
	
	public void addLife() {
		life++;
	}
	public int getLife() {
		return life;
	}
	
	public boolean hit(FlyingObject obj) {
		int x1 = obj.x - this.width / 2;
		int x2 = obj.x + obj.width + this.width / 2;
		int y1 = obj.y - this.height / 2;
		int y2 = obj.y + obj.height + this.height / 2;
		int x = this.x + this.width / 2;
		int y = this.y + this.height / 2;	
		return x >= x1 && x<= x2 && y >=y1 && y <= y2;
	}
	public void subLife() {
		life--;
	}
	public void clearDoubleFire() {
		doubleFire = 0;
	}
}
